package com.android.lovingpet.adopt.adoption;

import android.os.Bundle;
import android.util.Log;

import com.android.lovingpet.bean.JsonIds;
import com.android.lovingpet.common.Back;
import com.android.lovingpet.util.CallbackStore;
import com.android.lovingpet.util.MyDatePickDialog;
import com.android.lovingpet.util.MyHttp;
import com.android.lovingpet.util.URLStore;
import com.android.lovingpet.util.Utility;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by ikata on 2018/3/20.
 */

public class AdoptionModel {

    public void addAdopt(HashMap params,Back back)
    {
        MyHttp.getInstance().postAsync(Utility.getJSessionId(), URLStore.PET_ADDADOPT, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                back.onFailure(MyHttp.getFailureMeg());
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String responseData=response.body().string();
                Log.d("addAdopt",responseData);
                try {
                    int id=new JSONObject(responseData).getInt("id");
                    int code= MyHttp.getCode(responseData);
                    if (code==0) {
                        Bundle bundle = new Bundle();
                        bundle.putInt("id", id);
                        back.onSuccess(bundle);
                    }
                    else {
                        back.onFailure(MyHttp.getCodeString(code));
                    }
                } catch (JSONException e) {

                }
            }
        });
    }

    public void sort(HashMap<String,String> params,Back back)
    {
        MyHttp.getInstance().postAsync(Utility.getJSessionId(),URLStore.PET_SORT,params,new CallbackStore.CallbackNon<Back>(back)
        {
            @Override
            public void onResponse(String json) {
                int code= MyHttp.getCode(json);
                JsonIds jsonIds=new Gson().fromJson(json,JsonIds.class);
                Bundle bundle=new Bundle();
                bundle.putIntegerArrayList("ids",jsonIds.getIds());
                back.onSuccess(bundle);
            }
        });
    }

}
